package com.bj58.ecdata.dashboard.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

import com.bj58.ecdata.dashboard.databll.HomePageBLL;
import com.bj58.ecdata.dashboard.utils.MyServletRequestUtils;
import com.bj58.wf.mvc.ActionResult;
import com.bj58.wf.mvc.MvcController;
import com.bj58.wf.mvc.annotation.POST;
import com.bj58.wf.mvc.annotation.Path;

@Path("/homePage/ajax")
public class HomePageAjaxController extends MvcController{

	/**
	 * 余额客户结构
	 * @return
	 */
	@Path("/balance")
	@POST
	public ActionResult balance() {
		try {
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String cate = MyServletRequestUtils.getStringParameter(request, "cate", "");
			String userType = MyServletRequestUtils.getStringParameter(request, "userType", "");
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getUserBalanceData(cityName,businessName,cate,userType,stat_type,indexType);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getUserBalanceTableJson(cityName,businessName,cate,userType,stat_type,indexType,dateVal);
			}
//			 log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 收入
	 * @return
	 */
	@Path("/income")
	@POST
	public ActionResult income() {
		try {
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String cate = MyServletRequestUtils.getStringParameter(request, "cate", "");
			String userType = MyServletRequestUtils.getStringParameter(request, "userType", "");
//			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getIncomeData(cityName,businessName,cate,userType,stat_type);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getIncomeTableJson(cityName,businessName,cate,userType,stat_type,dateVal);
			}
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 流量
	 * @return
	 */
	@Path("/incomeNetflow")
	@POST
	public ActionResult incomeNetflow() {
		try {
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String cate = MyServletRequestUtils.getStringParameter(request, "cate", "");
			String proType = MyServletRequestUtils.getStringParameter(request, "proType", "");
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
						
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getIncomeNetflowData(cityName,businessName,cate,proType,stat_type,indexType);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getIncomeNetflowTableJson(cityName,businessName,cate,proType,stat_type,indexType,dateVal);
			}
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 消耗客户数
	 * @return
	 */
	@Path("/costUser")
	@POST
	public ActionResult costUser() {
		try {
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String cate = MyServletRequestUtils.getStringParameter(request, "cate", "");
			String proType = MyServletRequestUtils.getStringParameter(request, "proType", "");
			String userType = MyServletRequestUtils.getStringParameter(request, "userType", "");

			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getCostUserData(cityName,businessName,cate,proType,userType,stat_type);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getIncomeTableJson(cityName,businessName,cate,userType,stat_type,dateVal);
			}
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	
	
	
	/**
	 * 自然流量总体趋势
	 * @return
	 */
	@Path("/netflow/total")
	@POST
	public ActionResult getTotalData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String city = MyServletRequestUtils.getStringParameter(request, "city", "");
			
			String data = HomePageBLL.getNetFlowTotalData(city,businessName,indexType,stat_type);
//			log.info(cate+"--platform--"+index+"--"+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 自然流量三端趋势
	 * @return
	 */
	@Path("/netflow/platform")
	@POST
	public ActionResult getPlatformData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String city = MyServletRequestUtils.getStringParameter(request, "city", "");

			String data = HomePageBLL.getNetFlowPlatformJson(city,businessName,indexType,stat_type);
			//log.info("getPlatformData: "+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 自然流量类别趋势
	 * @return
	 */
	@Path("/netflow/cateFlow")
	@POST
	public ActionResult getCateNetflowData() {
		try {
			HttpServletRequest request = beat.getRequest();
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String city = MyServletRequestUtils.getStringParameter(request, "city", "");
			String cate = MyServletRequestUtils.getStringParameter(request, "cate", "");
			
			String data = HomePageBLL.getNetFlowCityCateJson(city,businessName,cate, indexType,stat_type);
			//log.info("getCateData: "+data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}

	/**
	 * 新增流失客户数
	 * @return
	 */
	@Path("/addLossUser")
	@POST
	public ActionResult addLossUser() {
		try {
			HttpServletRequest request = beat.getRequest();
			String statType = MyServletRequestUtils.getStringParameter(request, "statType", "week");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String cate = MyServletRequestUtils.getStringParameter(request, "cate", "");
			String proType = MyServletRequestUtils.getStringParameter(request, "proType", "");
			String vipType = MyServletRequestUtils.getStringParameter(request, "vipType", "");
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getAddLossUserData(cityName,businessName,cate,proType,vipType,statType);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getAddLossUserTableJson(cityName,businessName,cate,vipType,statType,dateVal);
			}
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 精准置顶状态转换客户数
	 * @return
	 */
	@Path("/transferUser")
	@POST
	public ActionResult transferUser() {
		try {
			HttpServletRequest request = beat.getRequest();
			String statType = MyServletRequestUtils.getStringParameter(request, "statType", "week");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String cate = MyServletRequestUtils.getStringParameter(request, "cate", "");
			String proType = MyServletRequestUtils.getStringParameter(request, "proType", "");
			String vipType = MyServletRequestUtils.getStringParameter(request, "vipType", "");

			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getTransferUserData(cityName,businessName,cate,proType,vipType,statType);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getTransferUserTableJson(cityName,businessName,cate,vipType,statType,dateVal); 
			}
			//log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 销售业绩
	 * @return
	 */
	@Path("/sales")
	@POST
	public ActionResult sales() {
		try {
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String teamType = MyServletRequestUtils.getStringParameter(request, "teamType", "");
			String pfmType = MyServletRequestUtils.getStringParameter(request, "pfmType", "");
			
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getSalesData(cityName,businessName,teamType,pfmType,stat_type);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getSalesTableJson(cityName,businessName,stat_type,dateVal,pfmType);
			}
			//log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 续费率
	 * @return
	 */
	@Path("/recharge")
	@POST
	public ActionResult recharge() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
		
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getRechargeData(cityName,businessName,indexType);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getRechargeTableJson(cityName,businessName,dateVal);
			}
			//log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 信息量变化趋势
	 * @return
	 */
	@Path("/info")
	@POST
	public ActionResult info() {
		try {
			
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cateName = MyServletRequestUtils.getStringParameter(request, "cateName", "");
			String proType = MyServletRequestUtils.getStringParameter(request, "proType", "");
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
			cateName = cateName.trim();
						
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getInfoData( businessName,  stat_type,  cateName,  proType, cityName);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getInfoTableJson(businessName,cityName,cateName,proType,stat_type,dateVal);
			}
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}

	/**
	 * 新增简历趋势
	 * @return
	 */
	@Path("/newResume")
	@POST
	public ActionResult newResume() {
      try {
			
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cateName = MyServletRequestUtils.getStringParameter(request, "cateName", "");
			String proType = MyServletRequestUtils.getStringParameter(request, "proType", "");
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");
        	cateName = cateName.trim();
			businessName = "zhaopin";		
        	
			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getNewResumeData(businessName, stat_type, cateName, proType, cityName);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){
				
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getNewResumeTableJson(businessName,cityName,cateName,proType,stat_type,dateVal);
			}
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 整体余额消耗
	 * @return
	 */
	@Path("/inventoryBalance")
	@POST
	public ActionResult inventoryBalance() {
      try {
			
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String cateName = MyServletRequestUtils.getStringParameter(request, "cateName", "all");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			String data="";
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getInventoryBalanceData(businessName, stat_type, indexType, cityName,cateName);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){				
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
				data = HomePageBLL.getInventoryBalanceTableJson(businessName, cityName,indexType, stat_type, dateVal,cateName);
			}
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 分业务线变化趋势
	 * @return
	 */
	@Path("/inventoryBusiness")
	@POST
	public ActionResult inventoryBusiness() {
      try {
			
			HttpServletRequest request = beat.getRequest();
			String stat_type = MyServletRequestUtils.getStringParameter(request, "statType", "day");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String cateName = MyServletRequestUtils.getStringParameter(request, "cateName", "all");
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String is_vipStr = MyServletRequestUtils.getStringParameter(request, "is_vip", "");
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "");
			String dataType = MyServletRequestUtils.getStringParameter(request, "dataType", "chart");

			String data="";
			int is_vip = Integer.parseInt(is_vipStr);
			if(StringUtils.equalsIgnoreCase(dataType, "chart")){
				data = HomePageBLL.getInventoryBusinessData(businessName, stat_type, is_vip, indexType, cityName);
			}else if(StringUtils.equalsIgnoreCase(dataType, "table")){				
				String dateVal = MyServletRequestUtils.getStringParameter(request, "dateVal", "2014-01-01");
			 	
			    data = HomePageBLL.getInventoryBusinessTableJson(businessName, cityName, cateName,is_vip, indexType, stat_type, dateVal);
			}
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 余额消耗率类别分布散点图
	 * @return
	 */
	@Path("/consume_rate")
	@POST
	public ActionResult consume_rate() {
		try {
			HttpServletRequest request = beat.getRequest();
			String businessName = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String cityName = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String vipType = MyServletRequestUtils.getStringParameter(request, "vipType", "");	
			String beginDate = MyServletRequestUtils.getStringParameter(request, "beginDate");			
			String indexType = MyServletRequestUtils.getStringParameter(request, "indexType", "1");
			
			String data="";				
			data = HomePageBLL.getConsumeRateData(cityName,businessName,vipType,beginDate,indexType);
			//log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
	
	/**
	 * 会员漏斗树图
	 * @return
	 */
	@Path("/vip_tree")
	@POST
	public ActionResult vip_tree() {
		try {
			HttpServletRequest request = beat.getRequest();
			String business_name = MyServletRequestUtils.getStringParameter(request, "businessName", "");
			String city_name = MyServletRequestUtils.getStringParameter(request, "cityName", "");
			String real_date = MyServletRequestUtils.getStringParameter(request, "realDate", "");
			if("".equals(real_date)){				
				real_date = HomePageBLL.getLastMonth(business_name,city_name);
			}else{			
				real_date = real_date.substring(0,4)+"-"+real_date.substring(4);
			}
			String data="";				
			data  = HomePageBLL.getVIPTreeData(business_name, city_name, real_date);
//			log.info(data);
			return ViewResult.jsonView(data);
		} catch (Exception e) {
			e.printStackTrace();
			return ViewResult.view("{}");
		}
	}
}
